package com.tjetc;

import com.tjetc.dao.IdCardDao;
import com.tjetc.dao.PersonDao;
import com.tjetc.dao.UserDao;
import com.tjetc.pojo.many.Pet;
import com.tjetc.pojo.many.User;
import com.tjetc.pojo.one.IdCard;
import com.tjetc.pojo.one.Person;
import com.tjetc.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.jupiter.api.Test;

import java.util.List;

/**
 * @author gaoyongxiang
 * @version v1.0
 * @date 2025/8/19
 */
public class _5OneToMany {


    // 测试mybatis的一对多映射
    @Test
    public void testOneToMany() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        // 调用方法获取数据
        User user = userDao.selectUserById(1);
        // 输出
        System.out.println(user);
        List<Pet> petList = user.getPetList();
        for (Pet pet : petList) {
            System.out.println(pet);
        }
    }


    // 测试嵌套查询
    @Test
    public void testSelect() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        User user = userDao.selectUserById1(1);
        System.out.println("user.getName() = " + user.getName());
        // StackOverflowError toString的连环调用
        // 解决：
        // 1.在实体类中将一个对象的toString 方法删除掉
        // 2.在输出的时候注意不要出现就可以了
        List<Pet> petList = user.getPetList();
        for (Pet pet : petList) {
            System.out.println(pet.getName());
        }
    }





}